package team.jaman.autosell.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Users;
import team.jaman.autosell.service.UsersService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.PageData;

@Controller
@RequestMapping("users")
public class UsersController {
	@Resource
	UsersService usersService;
	
	/**
	 * 根据账号密码查询用户
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public Users loginUsers(@RequestBody Users record) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		Users user = usersService.loginUsers(record, logger);
		System.out.println(logger);
		return user;
	}

	/**
	 * 添加用户
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public CommonData addUsers(@RequestBody Users record) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());
		CommonData commonData = usersService.addUsers(record,logger);
		System.out.println(commonData.getMsg());

		return commonData;
	}

	/**
	 * 修改用户
	 */
	@RequestMapping(value = "update", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
	@ResponseBody
	public CommonData updateUsers(@RequestBody Users record) {
		Logger logger = new Logger();
		logger.setStartTime(new Date());

		CommonData commonData = usersService.updateByPrimaryKeySelective(record,logger);
		System.out.println(commonData.getMsg());

		return commonData;
	}

	/**
	 * 查询用户
	 */
	@ResponseBody
	@RequestMapping(value = "selectLike", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	public PageData<Users> selectUsers(@RequestParam(value = "page", defaultValue = "1") int page,@RequestParam(value = "limit", defaultValue = "10")  int limit,@RequestParam(value = "keyword", defaultValue = "") String keyword) {
		
		System.out.println("keyword:" + keyword);
		PageData<Users> pageData = usersService.selectUsers(page, limit, keyword);
		pageData.getData().forEach(users -> System.out.println(users));

		return pageData;
	}

}
